package com.dgut.usermodule.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

public class LogListDTO {
	private Long logId; //日志id
	private String userName; //用户名
	private String moduleName; //模块名称
	private String operate; //方法名
	private String params;//传入的参数
	private String ip; //操作的ip地址
	public Long getLogId() {
		return logId;
	}
	public void setLogId(Long logId) {
		this.logId = logId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getModuleName() {
		return moduleName;
	}
	public void setModuleName(String moduleName) {
		this.moduleName = moduleName;
	}
	public String getOperate() {
		return operate;
	}
	public void setOperate(String operate) {
		this.operate = operate;
	}
	public String getParams() {
		return params;
	}
	public void setParams(String params) {
		this.params = params;
	}
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	public static Specification<Log> getSpecification(LogListDTO logListDTO){
		Specification<Log> specification = new Specification<Log>() {
			@Override
			public Predicate toPredicate(Root<Log> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				if (logListDTO != null && !StringUtils.isEmpty(logListDTO.getLogId())) {
					Predicate predicate = cb.like(root.get("logId").as(String.class), "%"+logListDTO.getLogId()+"%");
					list.add(predicate);
				}
				if (logListDTO != null && !StringUtils.isEmpty(logListDTO.getUserName())) {
					Predicate predicate = cb.like(root.get("userName").as(String.class), "%"+logListDTO.getUserName()+"%");
					list.add(predicate);
				}
				if (logListDTO != null && !StringUtils.isEmpty(logListDTO.getModuleName())) {
					Predicate predicate = cb.like(root.get("moduleName").as(String.class), "%"+logListDTO.getModuleName()+"%");
					list.add(predicate);
				}
				if (logListDTO != null && !StringUtils.isEmpty(logListDTO.getOperate())) {
					Predicate predicate = cb.like(root.get("operate").as(String.class), "%"+logListDTO.getOperate()+"%");
					list.add(predicate);
				}
				if (logListDTO != null && !StringUtils.isEmpty(logListDTO.getParams())) {
					Predicate predicate = cb.like(root.get("params").as(String.class), "%"+logListDTO.getParams()+"%");
					list.add(predicate);
				}
				if (logListDTO != null && !StringUtils.isEmpty(logListDTO.getIp())) {
					Predicate predicate = cb.like(root.get("ip").as(String.class), "%"+logListDTO.getIp()+"%");
					list.add(predicate);
				}
				Predicate[] predicates = new Predicate[list.size()];
				return cb.and(list.toArray(predicates));
			}
		};
		return specification;
	}
}
